package spring.cloud.service.task.domain.task;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import spring.cloud.service.task.dto.TaskDto;
import spring.cloud.tasks.common.curator.Curator;
import spring.cloud.tasks.common.curator.TransactionalCurator;
import spring.cloud.tasks.common.nodepaths.TaskBaseInfoNode;
import spring.cloud.tasks.common.nodepaths.TaskNodePaths;

@Slf4j
public final class ZooKeeperTaskConfigurationTransferUtil {

    public static void createTaskConfigurationToZooKeeper(Curator curator, TaskDto taskDto) throws RuntimeException {
        try {
            String taskId = taskDto.getTaskId();
            String taskType = taskDto.getTaskType();
            // 添加任务根节点
            curator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId), String.valueOf(taskDto.getUpdateVersion()));
            TransactionalCurator transactionalCurator = curator.getTransactionalCurator();
            // 添加task_configuration结点
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.description), StringUtils.trimToEmpty(taskDto.getDescription()));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.namespace_key), StringUtils.trimToEmpty(taskDto.getNamespaceKey()));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_type), StringUtils.trimToEmpty(taskDto.getTaskType()));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.xid), StringUtils.trimToEmpty(taskDto.getXid()));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_id), StringUtils.trimToEmpty(taskDto.getTaskId()));//
            //status代表启用和禁用 enable配置则代表是否任务进行调度。
            //如果在启用的情况下可以进行调度。如果在禁用的情况下则不能进行调度。
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.status), StringUtils.trimToEmpty(taskDto.getStatus()));//
            //transactionalCurator = transactionalCurator.create(TaskNode.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.prefer_executor_id_list), StringUtils.trimToEmpty(taskDto.getPreferExecutorIdList()));//第一次为空
            //
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.cron_expression), StringUtils.trimToEmpty(taskDto.getCronExpression()));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pause_period_date), StringUtils.trimToEmpty(taskDto.getPausePeriodDate()));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pause_period_time), StringUtils.trimToEmpty(taskDto.getPausePeriodTime()));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pre_allot_minutes), StringUtils.trimToEmpty(String.valueOf(taskDto.getPreAllotMinutes())));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.timeout_minutes), StringUtils.trimToEmpty(String.valueOf(taskDto.getTimeoutMinutes())));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.timeout_alarm_minutes), StringUtils.trimToEmpty(String.valueOf(taskDto.getTimeoutAlarmMinutes())));//
            //
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_properties), StringUtils.trimToEmpty(String.valueOf(taskDto.getTaskProperties())));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_data_rang_rule), StringUtils.trimToEmpty(taskDto.getTaskDataRangeRule()));//
            transactionalCurator = transactionalCurator.create(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_item_info), StringUtils.trimToEmpty(String.valueOf(taskDto.getTaskItemInfo())));//
            //这个字段是版本号 其他程序见到这个字段有被更新才进行数据处理
            curator.merge(TaskNodePaths.getTaskBaseInfoNodePath(taskId), System.currentTimeMillis() + "");
            transactionalCurator.commit();
        } catch (Exception e) {
            String tip = "添加任务配置信息到ZooKeeper失败";
            log.error(tip, e);
            throw new RuntimeException(tip, e);
        }
    }

    // 更新任务
    public static void updateTaskConfigurationToZooKeeper(Curator curator, TaskDto taskDto) throws RuntimeException {
        try {
            String taskId = taskDto.getTaskId();
            String taskType = taskDto.getTaskType();
            TransactionalCurator transactionalCurator = curator.getTransactionalCurator();
            //
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskNodePath(taskId), String.valueOf(taskDto.getUpdateVersion()));
            //
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.description), StringUtils.trimToEmpty(taskDto.getDescription()));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.namespace_key), StringUtils.trimToEmpty(taskDto.getNamespaceKey()));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_type), StringUtils.trimToEmpty(taskDto.getTaskType()));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.xid), StringUtils.trimToEmpty(taskDto.getXid()));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_id), StringUtils.trimToEmpty(taskDto.getTaskId()));//
            //status代表启用和禁用 enable配置则代表是否任务进行调度。
            //如果在启用的情况下可以进行调度。如果在禁用的情况下则不能进行调度。
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.status), StringUtils.trimToEmpty(taskDto.getStatus()));//
            //transactionalCurator = transactionalCurator.update(TaskNode.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.prefer_executor_id_list), StringUtils.trimToEmpty(taskDto.getPreferExecutorIdList()));//最新数据同步
            //
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.cron_expression), StringUtils.trimToEmpty(taskDto.getCronExpression()));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pause_period_date), StringUtils.trimToEmpty(taskDto.getPausePeriodDate()));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pause_period_time), StringUtils.trimToEmpty(taskDto.getPausePeriodTime()));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pre_allot_minutes), StringUtils.trimToEmpty(String.valueOf(taskDto.getPreAllotMinutes())));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.timeout_minutes), StringUtils.trimToEmpty(String.valueOf(taskDto.getTimeoutMinutes())));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.timeout_alarm_minutes), StringUtils.trimToEmpty(String.valueOf(taskDto.getTimeoutAlarmMinutes())));//
            //
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_properties), StringUtils.trimToEmpty(String.valueOf(taskDto.getTaskProperties())));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_data_rang_rule), StringUtils.trimToEmpty(String.valueOf(taskDto.getTaskDataRangeRule())));//
            transactionalCurator = transactionalCurator.update(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_item_info), StringUtils.trimToEmpty(taskDto.getTaskItemInfo()));//
            //
            curator.merge(TaskNodePaths.getTaskBaseInfoNodePath(taskId), System.currentTimeMillis() + "");
            transactionalCurator.commit();
        } catch (Exception e) {
            String tip = "更新任务配置信息到ZooKeeper失败";
            log.error(tip, e);
            throw new RuntimeException(tip, e);
        }
    }


    public static TaskDto getTaskConfigurationFromZooKeeper(Curator curator, String taskId) throws RuntimeException {
        String configNodePath = TaskNodePaths.getTaskBaseInfoNodePath(taskId);
        if (!curator.checkExists(configNodePath)) {
            return null;//
        }
        TaskDto taskDto = new TaskDto();
        taskDto.setUpdateVersion(Long.parseLong(curator.getData(TaskNodePaths.getTaskNodePath(taskId))));
        taskDto.setDescription(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.description)));
        taskDto.setNamespaceKey(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.namespace_key)));
        taskDto.setTaskType(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_type)));
        String taskType = taskDto.getTaskType();
        taskDto.setXid(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.xid)));
        taskDto.setTaskId(taskId);
        //
        taskDto.setStatus(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.status)));
        //taskDto.setPreferExecutorIdList(curator.getData(TaskNode.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.prefer_executor_id_list)));
        //
        taskDto.setCronExpression(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.cron_expression)));
        taskDto.setPausePeriodDate(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pause_period_date)));
        taskDto.setPausePeriodTime(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pause_period_time)));
        taskDto.setPreAllotMinutes(Integer.parseInt(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.pre_allot_minutes))));
        taskDto.setTimeoutMinutes(Integer.valueOf(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.timeout_minutes))));
        taskDto.setTimeoutAlarmMinutes(Integer.valueOf(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.timeout_alarm_minutes))));
        //
        taskDto.setTaskProperties(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_properties)));
        taskDto.setTaskDataRangeRule(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_data_rang_rule)));
        taskDto.setTaskItemInfo(curator.getData(TaskNodePaths.getTaskBaseInfoNodePath(taskId, TaskBaseInfoNode.task_item_info)));
        //

        return taskDto;
    }

    public static void updateTaskUpdateVersion(Curator curator) {
        String tasksUpdateVersionNodePath = TaskNodePaths.getTasksUpdateVersionNodePath();
        curator.merge(tasksUpdateVersionNodePath, String.valueOf(System.currentTimeMillis()));
    }
}
